package com.bubblecam.common;



import java.util.Random;

import com.bubblecam.activity.R;
import com.bubblecam.view.DisplayPanel;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Element {
	private int mX;
	private int mY;
	private int mXSpeed;
	private int mYSpeed;
	
	private Bitmap bitmap;
	public Element(Resources res,int mx,int my) {
		
		this.bitmap = BitmapFactory.decodeResource(res, R.drawable.ball);
		mX = mx-bitmap.getWidth()/2;
		mY = my-bitmap.getHeight()/2;
		Random rand = new Random();
		mXSpeed = rand.nextInt(7)-3;
		mYSpeed = rand.nextInt(7)-3;
		
		
	}
	public void doDraw(Canvas canvas)
	{
		canvas.drawBitmap(bitmap, mX, mY, null);
	}
	public void animate(long elapsedTime)
	{
		mX+=mXSpeed*(elapsedTime/20f);
		mY+=mYSpeed*(elapsedTime/20f);
		checkPosition();
	}
	public void stop()
	{
		mX = 0;
		mY = DisplayPanel.variablePosition;
	}
	public void checkPosition()
	{
		if(mX<=0)
		{
			mXSpeed = -mXSpeed;
			mX=0;
		}
		else if(mX+bitmap.getWidth() >= DisplayPanel.mWidth)
		{
			mXSpeed = -mXSpeed;
			mX = (int)DisplayPanel.mWidth-bitmap.getWidth();
		}
		if(mY<=0)
		{
			mYSpeed = -mYSpeed;
			mY=0;
		}
		else if(mY+bitmap.getHeight() >= DisplayPanel.mHeight)
		{
			mYSpeed = -mYSpeed;
			mY = (int)DisplayPanel.mHeight-bitmap.getHeight();
		}
		
			
	}
	
	
	

}
